//
//  YTWithdrawViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/27.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTWithdrawViewController.h"
#import "YTWithDrawMoneyViewModel.h"
#import "YTWithdrawAccountModel.h"
#import "YTAccountSelectViewController.h"

@interface YTWithdrawViewController ()<UIAlertViewDelegate>
// 账户信息
@property (weak, nonatomic) IBOutlet UILabel *accountNumText;
@property (weak, nonatomic) IBOutlet UILabel *nameText;
@property (weak, nonatomic) IBOutlet UILabel *typeText;
@property (weak, nonatomic) IBOutlet UIImageView *bgImage;
@property (weak, nonatomic) IBOutlet UITextField *moneyText;
@property (weak, nonatomic) IBOutlet UIButton *withdrawBtn;

@property (strong, nonatomic) YTWithDrawMoneyViewModel *viewModel;
@end

@implementation YTWithdrawViewController

- (void)initialize {
    _viewModel = [[YTWithDrawMoneyViewModel alloc] init];
    _moneyText.placeholder = [NSString stringWithFormat:@"本次最多可提现%@元", _moneyNum];
    @weakify(self);
    [_viewModel.withdrawMoney.enabled subscribeNext:^(NSNumber *enabled) {
        @strongify(self);
        self.withdrawBtn.backgroundColor = enabled.boolValue ? kMainBlueColor : [UIColor lightGrayColor];
        self.withdrawBtn.enabled = enabled.boolValue;
    }];
}

- (void)bindViewModel {
    RAC(_viewModel, moneyText) = _moneyText.rac_textSignal;
    RAC(_viewModel, max) = RACObserve(self, moneyNum);
    @weakify(self);
    [_viewModel.loadAccountList subscribeNext:^(NSMutableArray<YTWithdrawAccountModel *> *accountList) {
        @strongify(self);
        [self loadDefaultAccount:accountList[0]];
    }];
    
    [_viewModel.withdrawMoney.executing subscribeNext:[self yt_showHudWithTips:@"拼命提交中..."]];
    
    [[_viewModel.withdrawMoney.executionSignals switchToLatest] subscribeNext:^(id x) {
        [UIAlertView bk_showAlertViewWithTitle:@"友情提示" message:@"您的申请已收到\n请耐心等待哟🌟" cancelButtonTitle:@"确定" otherButtonTitles:@[] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    } error:^(NSError *error) {
        [SVProgressHUD showInfoWithStatus:@"提交失败了,请稍后重试"];
    }];
    
    [[_withdrawBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入提现密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
        [alert show];
    }];
}

- (void)loadDefaultAccount:(YTWithdrawAccountModel *)model {
    _bgImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"profile_withdraw%@", model.type]];
    _typeText.text = model.typeStr;
    self.viewModel.account_id = model.account_id;
    // 账户名
    NSString *firstChar = [model.name substringToIndex:1];
    NSString *lastChar = [model.name substringWithRange:NSMakeRange(model.name.length - 1, 1)];
    if (model.name.length == 2) {
        _nameText.text = [NSString stringWithFormat:@"*%@", lastChar];
    }
    
    if (model.name.length > 2) {
        NSString *lastChar = [model.name substringWithRange:NSMakeRange(model.name.length - 1, 1)];
        _nameText.text = [NSString stringWithFormat:@"%@*%@", firstChar, lastChar];
    }
    
    // 账号,除了银行账号需要做隐藏,其他都不需要
    if ([model.type isEqualToString:@"3"]) {
        NSString *front = [model.number substringToIndex:5];
        NSString *back = [model.number substringWithRange:NSMakeRange(model.number.length - 4, 4)];
        _accountNumText.text = [NSString stringWithFormat:@"%@****%@", front, back];
    } else {
        _accountNumText.text = model.number;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 1) return;
    [self.viewModel.withdrawMoney execute:[alertView textFieldAtIndex:0].text];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTSelectWithdrawAccount"]) {
        YTAccountSelectViewController *account = segue.destinationViewController;
        account.accountList = _viewModel.accountList;
        account.callBackAction = [RACSubject subject];
        [account.callBackAction subscribeNext:^(YTWithdrawAccountModel *model) {
            [self loadDefaultAccount:model];
        }];
    }
}

@end
